﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_User_User_Update : System.Web.UI.Page
{
    //接收用户id参数变量
    int id = 0;
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
    JobManager dllJobManager = new JobManager();
    RoleManager dllRoleManager = new RoleManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Convert.ToInt32(Request.QueryString["ID"].ToString());
        if (!IsPostBack)
        {           
            BindDropDownList();
            Bind();
        }
    }

    /// <summary>
    /// 绑定具体的值
    /// </summary>
    void Bind()
    {
        object[] _value = new object[1];
        _value[0] = id;
        DataTable user_table = dllUserInfoManager.get(UserInfoManager.field.UserId.ToString(), _value);
        this.txtUserName.Text = user_table.Rows[0]["UserName"].ToString();
        this.txtRealName.Text = user_table.Rows[0]["RealName"].ToString();
        this.txtAge.Text = user_table.Rows[0]["Age"].ToString();
        //绑定该用户的部门
        this.drpDepartmentId.SelectedIndex= this.drpDepartmentId.Items.IndexOf(this.drpDepartmentId.Items.FindByText(user_table.Rows[0]["DepartmentId"].ToString()));
        //绑定该用户的主岗
        ListItem masterjob = new ListItem(user_table.Rows[0]["MasterJob"].ToString());
        this.drpMasterJob.Items.Insert(0, masterjob);
        //绑定该用户的兼职部门
        this.drpPartTimeDepartmentId.SelectedIndex = this.drpPartTimeDepartmentId.Items.IndexOf(this.drpPartTimeDepartmentId.Items.FindByText(user_table.Rows[0]["PartTimeDepartmentId"].ToString()));
        //绑定该用户的副岗
        ListItem slavejob = new ListItem(user_table.Rows[0]["SlaveJob"].ToString());
        this.drpSlaveJob.Items.Insert(0, slavejob);
        //绑定该用户的职务
        this.drpPostLevel.SelectedIndex = this.drpPostLevel.Items.IndexOf(this.drpPostLevel.Items.FindByText(user_table.Rows[0]["PostLevel"].ToString()));
        //绑定该用户的主管
        this.drpIsDepartmentManager.SelectedIndex = this.drpIsDepartmentManager.Items.IndexOf(this.drpIsDepartmentManager.Items.FindByText(user_table.Rows[0]["IsDepartmentManager"].ToString()));
        //绑定该用户的性别
        this.drpSex.SelectedIndex = this.drpSex.Items.IndexOf(this.drpSex.Items.FindByText(user_table.Rows[0]["Sex"].ToString()));
        //绑定角色组
        this.drpRoleName.SelectedIndex = this.drpRoleName.Items.IndexOf(this.drpRoleName.Items.FindByText(user_table.Rows[0]["RoleName"].ToString()));
    }



    /// <summary>
    /// 绑定所有的DropDownList
    /// </summary>
    void BindDropDownList()
    {
        //绑定部门
        DataTable Department = dllDepartmentInfoManager.get();
        this.drpDepartmentId.DataSource = Department;
        this.drpDepartmentId.DataTextField = "DepartmentName";
        this.drpDepartmentId.DataValueField = "DepartmentId";
        this.drpDepartmentId.DataBind();
        ListItem department = new ListItem("请选择部门");
        this.drpDepartmentId.Items.Insert(0, department);

        this.drpPartTimeDepartmentId.DataSource = Department;
        this.drpPartTimeDepartmentId.DataTextField = "DepartmentName";
        this.drpPartTimeDepartmentId.DataValueField = "DepartmentId";
        this.drpPartTimeDepartmentId.DataBind();
        ListItem PartTimeDepartment = new ListItem("请选择兼职部门");
        this.drpPartTimeDepartmentId.Items.Insert(0, PartTimeDepartment);

        //绑定岗位
        ListItem masterjob = new ListItem("请选择");
        this.drpMasterJob.Items.Insert(0, masterjob);
        ListItem slavejob = new ListItem("请选择");
        this.drpSlaveJob.Items.Insert(0, slavejob);

        //绑定角色
        //绑定角色
        this.drpRoleName.DataSource = dllRoleManager.get();
        this.drpRoleName.DataTextField = "RoleName";
        this.drpRoleName.DataValueField = "RoleId";
        this.drpRoleName.DataBind();

    }

    /// <summary>
    /// 提交事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-6</date>
    protected void btntijao_Click(object sender, EventArgs e)
    {
        object[] _value = new object[12];//定义一个数组
        _value[0] = id;
        _value[1] = txtUserName.Text;
        _value[2] = int.Parse(drpDepartmentId.SelectedValue.ToString());
        _value[3] = int.Parse(drpPartTimeDepartmentId.SelectedValue.ToString());
        _value[4] = drpMasterJob.SelectedItem.Text;
        _value[5] = drpSlaveJob.SelectedItem.Text;
        _value[6] = drpPostLevel.SelectedItem.Text;
        _value[7] = int.Parse(drpIsDepartmentManager.SelectedValue.ToString());
        _value[8] = txtRealName.Text;
        _value[9] = Convert.ToInt32(txtAge.Text);
        _value[10] = drpSex.SelectedValue;
        _value[11] = this.drpRoleName.SelectedItem.Text;
        int result = dllUserInfoManager.edit(UserInfoManager.field.UserId.ToString(),_value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("修改用户未成功，请重试！");
        }
        else
        {
            Response.Redirect("User_Manager.aspx");
        }
    }


    /// <summary>
    /// 返回事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnchongzhi_Click(object sender, EventArgs e)
    {
        Response.Redirect("User_Manager.aspx");
    }



    /// <summary>
    /// 部门下拉改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpDepartmentId_SelectedIndexChanged(object sender, EventArgs e)
    {
        //清除绑定项
        this.drpMasterJob.Items.Clear();
        if (this.drpDepartmentId.SelectedItem.Text == "请选择部门")
        {
            ListItem li = new ListItem("请选择");
            this.drpMasterJob.Items.Insert(0, li);
        }
        else
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.drpDepartmentId.SelectedValue);
            DataTable job = dllJobManager.get();
            if (job.Rows.Count > 0)
            {
                this.drpMasterJob.DataSource = job;
                this.drpMasterJob.DataTextField = "JobName";
                this.drpMasterJob.DataValueField = "JobId";
                this.drpMasterJob.DataBind();
            }
            else
            {

                ListItem li = new ListItem("该部门无岗位");
                this.drpMasterJob.Items.Insert(0, li);
            }
        }

    }

    /// <summary>
    /// 兼职部门下拉改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpPartTimeDepartmentId_SelectedIndexChanged(object sender, EventArgs e)
    {
        //清除绑定项
        this.drpSlaveJob.Items.Clear();
        if (this.drpPartTimeDepartmentId.SelectedItem.Text == "请选择兼职部门")
        {
            ListItem li = new ListItem("请选择");
            this.drpSlaveJob.Items.Insert(0, li);
            this.drpSlaveJob.DataBind();
        }
        else
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.drpPartTimeDepartmentId.SelectedValue);
            DataTable job = dllJobManager.get();
            if (job.Rows.Count > 0)
            {
                this.drpSlaveJob.DataSource = job;
                this.drpSlaveJob.DataTextField = "JobName";
                this.drpSlaveJob.DataValueField = "JobId";
                this.drpSlaveJob.DataBind();
            }
            else
            {

                ListItem li = new ListItem("该部门无岗位");
                this.drpSlaveJob.Items.Insert(0, li);
            }
        }
    }
}
